
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>IEx辅助函数 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../advanced/erlang.html" />
    
    
    <link rel="prev" href="mix-tasks.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">第一部分：基础</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="basics.html">
            
                <a href="basics.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="collections.html">
            
                <a href="collections.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="enum.html">
            
                <a href="enum.html">
            
                    
                    Enum 模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="pattern-matching.html">
            
                <a href="pattern-matching.html">
            
                    
                    模式匹配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="control-structures.html">
            
                <a href="control-structures.html">
            
                    
                    控制语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="functions.html">
            
                <a href="functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="pipe-operator.html">
            
                <a href="pipe-operator.html">
            
                    
                    管道操作符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="modules.html">
            
                <a href="modules.html">
            
                    
                    模块(Module)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="mix.html">
            
                <a href="mix.html">
            
                    
                    Mix
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="sigils.html">
            
                <a href="sigils.html">
            
                    
                    魔符(Sigil)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="documentation.html">
            
                <a href="documentation.html">
            
                    
                    文档模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="testing.html">
            
                <a href="testing.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="comprehensions.html">
            
                <a href="comprehensions.html">
            
                    
                    推导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="strings.html">
            
                <a href="strings.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="date-time.html">
            
                <a href="date-time.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="mix-tasks.html">
            
                <a href="mix-tasks.html">
            
                    
                    自定义Mix任务
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.18" data-path="iex-helpers.html">
            
                <a href="iex-helpers.html">
            
                    
                    IEx辅助函数
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第二部分：高级</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../advanced/erlang.html">
            
                <a href="../advanced/erlang.html">
            
                    
                    和Erlang互操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../advanced/error-handling.html">
            
                <a href="../advanced/error-handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../advanced/escripts.html">
            
                <a href="../advanced/escripts.html">
            
                    
                    可执行文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../advanced/concurrency.html">
            
                <a href="../advanced/concurrency.html">
            
                    
                    并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../advanced/otp-concurrency.html">
            
                <a href="../advanced/otp-concurrency.html">
            
                    
                    OTP并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../advanced/otp-supervisors.html">
            
                <a href="../advanced/otp-supervisors.html">
            
                    
                    OTP Supervisors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="../advanced/otp-distribution.html">
            
                <a href="../advanced/otp-distribution.html">
            
                    
                    OTP 分布式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="../advanced/metaprogramming.html">
            
                <a href="../advanced/metaprogramming.html">
            
                    
                    元编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="../advanced/umbrella-projects.html">
            
                <a href="../advanced/umbrella-projects.html">
            
                    
                    Umbrella Projects
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="../advanced/typespec.html">
            
                <a href="../advanced/typespec.html">
            
                    
                    Specifications and types
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="../advanced/behaviours.html">
            
                <a href="../advanced/behaviours.html">
            
                    
                    行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="../advanced/gen-stage.html">
            
                <a href="../advanced/gen-stage.html">
            
                    
                    GenStage
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="../advanced/protocols.html">
            
                <a href="../advanced/protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.14" data-path="../advanced/nerves.html">
            
                <a href="../advanced/nerves.html">
            
                    
                    Nerves
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第三部分：ECTO</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../ecto/basics.html">
            
                <a href="../ecto/basics.html">
            
                    
                    Basics
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../ecto/changesets.html">
            
                <a href="../ecto/changesets.html">
            
                    
                    Changesets
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../ecto/associations.html">
            
                <a href="../ecto/associations.html">
            
                    
                    关联关系
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../ecto/querying.html">
            
                <a href="../ecto/querying.html">
            
                    
                    查询
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第四部分：专题</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../specifics/plug.html">
            
                <a href="../specifics/plug.html">
            
                    
                    Plug
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../specifics/eex.html">
            
                <a href="../specifics/eex.html">
            
                    
                    嵌入的 Elixir(EEx)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../specifics/ets.html">
            
                <a href="../specifics/ets.html">
            
                    
                    Erlang 项式存储(ETS)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../specifics/mnesia.html">
            
                <a href="../specifics/mnesia.html">
            
                    
                    Mnesia 数据库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../specifics/debugging.html">
            
                <a href="../specifics/debugging.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第五部分：程序库</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../libraries/guardian.html">
            
                <a href="../libraries/guardian.html">
            
                    
                    Guardian(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../libraries/poolboy.html">
            
                <a href="../libraries/poolboy.html">
            
                    
                    Poolboy
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../libraries/benchee.html">
            
                <a href="../libraries/benchee.html">
            
                    
                    Benchee
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../libraries/bypass.html">
            
                <a href="../libraries/bypass.html">
            
                    
                    Bypass
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="../libraries/distillery.html">
            
                <a href="../libraries/distillery.html">
            
                    
                    Distillery(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="../libraries/stream-data.html">
            
                <a href="../libraries/stream-data.html">
            
                    
                    StreamData
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >IEx辅助函数</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x6982;&#x8FF0;">&#x6982;&#x8FF0;</h2>
<p>&#x5F53;&#x4F60;&#x5F00;&#x59CB;&#x4F7F;&#x7528;Elixir&#x65F6;, IEx&#x662F;&#x4F60;&#x6700;&#x597D;&#x7684;&#x670B;&#x53CB;.
IEx&#x4E0D;&#x4EC5;&#x4EC5;&#x662F;&#x4E00;&#x4E2A;REPL&#x7A0B;&#x5E8F;, &#x5B83;&#x8FD8;&#x62E5;&#x6709;&#x5F88;&#x591A;&#x9AD8;&#x7EA7;&#x529F;&#x80FD;&#x8BA9;&#x6D4F;&#x89C8;&#x6216;&#x8005;&#x5F00;&#x53D1;&#x4EE3;&#x7801;&#x53D8;&#x5F97;&#x66F4;&#x7B80;&#x5355;.
&#x6211;&#x4EEC;&#x5C06;&#x5728;&#x8FD9;&#x8282;&#x8BFE;&#x5B66;&#x4E60;&#x90E8;&#x5206;&#x5185;&#x5EFA;&#x7684;&#x8F85;&#x52A9;&#x51FD;&#x6570;.</p>
<h3 id="&#x81EA;&#x52A8;&#x8865;&#x5168;">&#x81EA;&#x52A8;&#x8865;&#x5168;</h3>
<p>&#x5F53;&#x5728;shell&#x4E2D;&#x5DE5;&#x4F5C;&#x65F6;, &#x7ECF;&#x5E38;&#x4F1A;&#x9047;&#x5230;&#x4E0D;&#x719F;&#x6089;&#x7684;&#x6A21;&#x5757;.
&#x4E3A;&#x4E86;&#x4E86;&#x89E3;&#x6A21;&#x5757;&#x5982;&#x4F55;&#x4F7F;&#x7528;, &#x81EA;&#x52A8;&#x8865;&#x5168;&#x7684;&#x529F;&#x80FD;&#x5C31;&#x663E;&#x5F97;&#x975E;&#x5E38;&#x6709;&#x7528;.
&#x53EA;&#x9700;&#x8981;&#x5728;&#x8F93;&#x5165;&#x6A21;&#x5757;&#x540D;&#x548C;<code>.</code>&#x540E;, &#x6309;&#x4E0B; <code>Tab</code>:</p>
<pre><code class="lang-elixir">iex&gt; Map. # press Tab
delete/2             drop/2               equal?/2
fetch!/2             fetch/2              from_struct/1
get/2                get/3                get_and_update!/3
get_and_update/3     get_lazy/3           has_key?/2
keys/1               merge/2              merge/3
new/0                new/1                new/2
pop/2                pop/3                pop_lazy/3
put/3                put_new/3            put_new_lazy/3
replace!/3           replace/3            split/2
take/2               to_list/1            update!/3
update/4             values/1
</code></pre>
<p>&#x8FD9;&#x6837;&#x6211;&#x4EEC;&#x5C31;&#x77E5;&#x9053;&#x6A21;&#x5757;&#x4E2D;&#x6709;&#x90A3;&#x4E9B;&#x51FD;&#x6570;&#x4E86;!</p>
<h3 id="iexexs"><code>.iex.exs</code></h3>
<p>&#x6BCF;&#x5F53; IEx &#x542F;&#x52A8;&#x65F6;&#x90FD;&#x4F1A;&#x8BFB;&#x53D6;&#x53EB; <code>.iex.exs</code> &#x7684;&#x914D;&#x7F6E;&#x6587;&#x4EF6;. &#x8FD9;&#x4E2A;&#x6587;&#x4EF6;&#x4E0D;&#x5728;&#x5F53;&#x524D;&#x76EE;&#x5F55;&#x65F6;, &#x4F1A;&#x4F7F;&#x7528;&#x7528;&#x6237;&#x5BB6;&#x76EE;&#x5F55;&#x7684;&#x6587;&#x4EF6; (<code>~/.iex.exs</code>) &#x4F5C;&#x4E3A;&#x4EE3;&#x66FF;.</p>
<p>IEx shell&#x542F;&#x52A8;&#x540E;, &#x8FD9;&#x4E2A;&#x6587;&#x4EF6;&#x4E2D;&#x5B9A;&#x4E49;&#x7684;&#x914D;&#x7F6E;&#x9879;&#x548C;&#x4EE3;&#x7801;&#x53EF;&#x4EE5;&#x88AB;&#x6211;&#x4EEC;&#x4F7F;&#x7528;. &#x6BD4;&#x5982;&#x60F3;&#x5728; IEx &#x4E2D;&#x4F7F;&#x7528;&#x4E00;&#x4E9B;&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x7684;&#x8F85;&#x52A9;&#x51FD;&#x6570;, &#x5C31;&#x53EF;&#x4EE5;&#x6253;&#x5F00; <code>.iex.exs</code> &#x8FDB;&#x884C;&#x4E00;&#x4E9B;&#x66F4;&#x6539;.</p>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x5F00;&#x59CB;&#x521B;&#x5EFA;&#x4E00;&#x4E9B;&#x8F85;&#x52A9;&#x51FD;&#x6570;&#x5427;:</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">IExHelpers</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">whats_this?</span></span>(term) <span class="hljs-keyword">when</span> is_nil(term), <span class="hljs-symbol">do:</span> <span class="hljs-string">&quot;Type: Nil&quot;</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">whats_this?</span></span>(term) <span class="hljs-keyword">when</span> is_binary(term), <span class="hljs-symbol">do:</span> <span class="hljs-string">&quot;Type: Binary&quot;</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">whats_this?</span></span>(term) <span class="hljs-keyword">when</span> is_boolean(term), <span class="hljs-symbol">do:</span> <span class="hljs-string">&quot;Type: Boolean&quot;</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">whats_this?</span></span>(term) <span class="hljs-keyword">when</span> is_atom(term), <span class="hljs-symbol">do:</span> <span class="hljs-string">&quot;Type: Atom&quot;</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">whats_this?</span></span>(_term), <span class="hljs-symbol">do:</span> <span class="hljs-string">&quot;Type: Unknown&quot;</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x73B0;&#x5728;&#x5F53;&#x6211;&#x4EEC;&#x542F;&#x52A8; IEx &#x7684;&#x65F6;&#x5019;&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; IExHelpers &#x6A21;&#x5757;&#x4E86;. &#x542F;&#x52A8; IEx &#x8BD5;&#x4E00;&#x4E0B;&#x6211;&#x4EEC;&#x7684;&#x8F85;&#x52A9;&#x51FD;&#x6570;:</p>
<pre><code class="lang-elixir"><span class="hljs-variable">$ </span>iex
{{ site.erlang.OTP }} [{{ site.erlang.erts }}] [source] [<span class="hljs-number">64</span>-bit] [<span class="hljs-symbol">smp:</span><span class="hljs-number">8</span><span class="hljs-symbol">:</span><span class="hljs-number">8</span>] [async-<span class="hljs-symbol">threads:</span><span class="hljs-number">10</span>] [hipe] [kernel-<span class="hljs-symbol">poll:</span><span class="hljs-keyword">false</span>] [dtrace]

Interactive Elixir ({{ site.elixir.version }}) - press Ctrl+C to exit (type h() ENTER <span class="hljs-keyword">for</span> help)
iex&gt; IExHelpers.whats_this?(<span class="hljs-string">&quot;a string&quot;</span>)
<span class="hljs-string">&quot;Type: Binary&quot;</span>
iex&gt; IExHelpers.whats_this?(%{})
<span class="hljs-string">&quot;Type: Unknown&quot;</span>
iex&gt; IExHelpers.whats_this?(<span class="hljs-symbol">:test</span>)
<span class="hljs-string">&quot;Type: Atom&quot;</span>
</code></pre>
<p>&#x5982;&#x6211;&#x4EEC;&#x6240;&#x89C1;, &#x5E76;&#x4E0D;&#x9700;&#x8981;&#x7279;&#x522B;&#x7684; <code>require</code> &#x6216; <code>import</code>, IEx &#x5DF2;&#x7ECF;&#x5E2E;&#x6211;&#x4EEC;&#x5904;&#x7406;&#x597D;&#x4E86;.</p>
<h3 id="h"><code>h</code></h3>
<p><code>h</code> &#x662F; Elixir shell &#x4E2D;&#x6700;&#x6709;&#x7528;&#x7684;&#x5DE5;&#x5177;&#x4E86;.
&#x591A;&#x4E8F;&#x4E86;&#x8FD9;&#x95E8;&#x8BED;&#x8A00;&#x5BF9;&#x6587;&#x6863;&#x7684;&#x652F;&#x6301;&#x4E00;&#x7EA7;&#x68D2;, &#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x7528;&#x8FD9;&#x4E2A;&#x8F85;&#x52A9;&#x51FD;&#x6570;&#x67E5;&#x770B;&#x4EFB;&#x610F;&#x4EE3;&#x7801;&#x7684;&#x6587;&#x6863;.
&#x6211;&#x4EEC;&#x770B;&#x4E00;&#x4E0B;&#x4F7F;&#x7528;&#x7684;&#x4F8B;&#x5B50;:</p>
<pre><code class="lang-elixir">iex&gt; h Enum
                                      Enum

Provides a set of algorithms that enumerate over enumerables according to the
Enumerable protocol.

&#x2503; iex&gt; Enum.map([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>], <span class="hljs-keyword">fn</span>(x) -&gt; x * <span class="hljs-number">2</span> <span class="hljs-keyword">end</span>)
&#x2503; [<span class="hljs-number">2</span>, <span class="hljs-number">4</span>, <span class="hljs-number">6</span>]

Some particular types, like maps, yield a specific format on enumeration. For
example, the argument is always a {key, value} tuple <span class="hljs-keyword">for</span> <span class="hljs-symbol">maps:</span>

&#x2503; iex&gt; map = %{<span class="hljs-symbol">a:</span> <span class="hljs-number">1</span>, <span class="hljs-symbol">b:</span> <span class="hljs-number">2</span>}
&#x2503; iex&gt; Enum.map(map, <span class="hljs-keyword">fn</span> {k, v} -&gt; {k, v * <span class="hljs-number">2</span>} <span class="hljs-keyword">end</span>)
&#x2503; [<span class="hljs-symbol">a:</span> <span class="hljs-number">2</span>, <span class="hljs-symbol">b:</span> <span class="hljs-number">4</span>]

Note that the functions <span class="hljs-keyword">in</span> the Enum <span class="hljs-keyword">module</span> are <span class="hljs-symbol">eager:</span> they always start the
enumeration of the given enumerable. The Stream <span class="hljs-keyword">module</span> allows lazy enumeration
of enumerables <span class="hljs-keyword">and</span> provides infinite streams.

Since the majority of the functions <span class="hljs-keyword">in</span> Enum enumerate the whole enumerable <span class="hljs-keyword">and</span>
<span class="hljs-keyword">return</span> a list as result, infinite streams need to be carefully used with such
functions, as they can potentially run forever. For <span class="hljs-symbol">example:</span>

&#x2503; Enum.each Stream.cycle([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]), &amp;IO.puts(&amp;<span class="hljs-number">1</span>)
</code></pre>
<p>&#x751A;&#x81F3;&#x53EF;&#x4EE5;&#x5728; shell &#x4E2D;&#x4E0E;&#x8865;&#x5168;&#x529F;&#x80FD;&#x7EC4;&#x5408;&#x5728;&#x4E00;&#x8D77;&#x4F7F;&#x7528;.
&#x5047;&#x60F3;&#x4E00;&#x4E0B;&#x8FD9;&#x662F;&#x6211;&#x4EEC;&#x7B2C;&#x4E00;&#x6B21;&#x4F7F;&#x7528; Map:</p>
<pre><code class="lang-elixir">iex&gt; h Map
                                      Map

A set of functions for working with maps.

Maps are key-value stores where keys can be any value and are compared using
the match operator (===). Maps can be created with the %{} special form defined
in the Kernel.SpecialForms module.

iex&gt; Map.
delete/2             drop/2               equal?/2
fetch!/2             fetch/2              from_struct/1
get/2                get/3                get_and_update!/3
get_and_update/3     get_lazy/3           has_key?/2
keys/1               merge/2              merge/3
new/0                new/1                new/2
pop/2                pop/3                pop_lazy/3
put/3                put_new/3            put_new_lazy/3
split/2              take/2               to_list/1
update!/3            update/4             values/1

iex&gt; h Map.merge/2
                             def merge(map1, map2)

Merges two maps into one.

All keys in map2 will be added to map1, overriding any existing one.

If you have a struct and you would like to merge a set of keys into the struct,
do not use this function, as it would merge all keys on the right side into the
struct, even if the key is not part of the struct. Instead, use
Kernel.struct/2.

Examples

&#x2503; iex&gt; Map.merge(%{a: 1, b: 2}, %{a: 3, d: 4})
&#x2503; %{a: 3, b: 2, d: 4}
</code></pre>
<p>&#x5982;&#x6211;&#x4EEC;&#x6240;&#x89C1;, &#x4E0D;&#x4EC5;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x6A21;&#x5757;&#x4E2D;&#x6709;&#x4EC0;&#x4E48;&#x51FD;&#x6570;&#x8FD8;&#x80FD;&#x770B;&#x5230;&#x6BCF;&#x4E2A;&#x5355;&#x72EC;&#x51FD;&#x6570;&#x7684;&#x6587;&#x6863;, &#x800C;&#x4E14;&#x5B83;&#x4EEC;&#x5927;&#x591A;&#x90FD;&#x6709;&#x4F7F;&#x7528;&#x793A;&#x4F8B;.</p>
<h3 id="i"><code>i</code></h3>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x7528;&#x521A;&#x624D;&#x5B66;&#x5230;&#x7684; <code>h</code> &#x6765; &#x4E86;&#x89E3;&#x4E00;&#x4E0B; <code>i</code> &#x8F85;&#x52A9;&#x51FD;&#x6570;:</p>
<pre><code class="lang-elixir">iex&gt; h i

                                  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">i</span></span>(term)

Prints information about the given data type.

iex&gt; i Map
Term
  Map
Data type
  Atom
Module bytecode
  /usr/local/Cellar/elixir/<span class="hljs-number">1.3</span>.<span class="hljs-number">3</span>/bin/../lib/elixir/ebin/Elixir.Map.beam
Source
  /private/tmp/elixir-<span class="hljs-number">20160918</span>-<span class="hljs-number">33925</span>-<span class="hljs-number">1</span>ki46ng/elixir-<span class="hljs-number">1.3</span>.<span class="hljs-number">3</span>/lib/elixir/lib/map.ex
Version
  [<span class="hljs-number">9651177287794427227743899018880159024</span>]
Compile time
  no value found
Compile options
  [<span class="hljs-symbol">:debug_info</span>]
Description
  Use h(Map) to access its documentation.
  Call Map.module_info() to access metadata.
Raw representation
  <span class="hljs-symbol">:<span class="hljs-string">&quot;Elixir.Map&quot;</span></span>
Reference modules
  Module, Atom
</code></pre>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x5F97;&#x5230;&#x4E86;&#x4E00;&#x5806;&#x5173;&#x4E8E; <code>Map</code> &#x7684;&#x4FE1;&#x606F;, &#x5305;&#x62EC;&#x6E90;&#x6587;&#x4EF6;&#x8DEF;&#x5F84;&#x548C;&#x6D89;&#x53CA;&#x5230;&#x7684;&#x5176;&#x4ED6;&#x6A21;&#x5757;. &#x65E0;&#x8BBA;&#x6D4F;&#x89C8;&#x81EA;&#x5B9A;&#x4E49;&#x8FD8;&#x662F;&#x5916;&#x6765;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x6216;&#x51FD;&#x6570;&#x65F6;&#x90FD;&#x5F88;&#x6709;&#x5E2E;&#x52A9;.</p>
<p>&#x5355;&#x72EC;&#x7684;&#x6BCF;&#x6BB5;&#x6587;&#x5B57;&#x770B;&#x7740;&#x90FD;&#x5F88;&#x591A;, &#x4F46;&#x603B;&#x7684;&#x6765;&#x8BF4;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x83B7;&#x53D6;&#x8FD9;&#x4E9B;&#x6709;&#x610F;&#x4E49;&#x7684;&#x4FE1;&#x606F;:</p>
<ul>
<li>&#x8FD9;&#x662F;&#x4E00;&#x4E2A; atom &#x6570;&#x636E;&#x7C7B;&#x578B;</li>
<li>&#x6E90;&#x4EE3;&#x7801;&#x6240;&#x5728;&#x4F4D;&#x7F6E;</li>
<li>&#x7248;&#x672C;&#x548C;&#x7F16;&#x8BD1;&#x9009;&#x9879;</li>
<li>&#x57FA;&#x672C;&#x63CF;&#x8FF0;</li>
<li>&#x539F;&#x59CB;&#x8868;&#x793A;&#x5F62;&#x5F0F;</li>
<li>&#x6D89;&#x53CA;&#x5230;&#x7684;&#x5176;&#x5B83;&#x6A21;&#x5757;</li>
</ul>
<p>&#x8FD9;&#x7ED9;&#x4E86;&#x6211;&#x4EEC;&#x5F88;&#x591A;&#x53EF;&#x4F9B;&#x53C2;&#x8003;&#x7684;&#x4FE1;&#x606F;, &#x7EC8;&#x4E8E;&#x53EF;&#x4EE5;&#x4E0D;&#x7528;&#x6293;&#x778E;&#x4E86;.</p>
<h3 id="r"><code>r</code></h3>
<p>&#x5982;&#x679C;&#x60F3;&#x8981;&#x91CD;&#x65B0;&#x7F16;&#x8BD1;&#x4E00;&#x4E2A;&#x5177;&#x4F53;&#x7684;&#x6A21;&#x5757;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>r</code> &#x8F85;&#x52A9;&#x51FD;&#x6570;. &#x6BD4;&#x5982;&#x6539;&#x53D8;&#x4E86;&#x4E00;&#x70B9;&#x4EE3;&#x7801;&#x60F3;&#x8981;&#x8FD0;&#x884C;&#x65B0;&#x52A0;&#x7684;&#x51FD;&#x6570;, &#x53EF;&#x4EE5;&#x4FDD;&#x5B58;&#x53D8;&#x66F4;&#x540E;&#x4F7F;&#x7528; <code>r</code> &#x6765;&#x91CD;&#x65B0;&#x7F16;&#x8BD1;:</p>
<pre><code class="lang-elixir">iex&gt; r MyProject
<span class="hljs-symbol">warning:</span> redefining <span class="hljs-keyword">module</span> MyProject (current version loaded from _build/dev/lib/my_project/ebin/Elixir.MyProject.beam)
  lib/my_project.<span class="hljs-symbol">ex:</span><span class="hljs-number">1</span>

{<span class="hljs-symbol">:reloaded</span>, MyProject, [MyProject]}
</code></pre>
<h3 id="t"><code>t</code></h3>
<p><code>t</code> &#x8F85;&#x52A9;&#x51FD;&#x6570;&#x544A;&#x8BC9;&#x6211;&#x4EEC;&#x5728;&#x6A21;&#x5757;&#x4E2D;&#x90FD;&#x6709;&#x90A3;&#x4E9B;&#x5B9A;&#x4E49;&#x7684;&#x7C7B;&#x578B;:</p>
<pre><code class="lang-elixir">iex&gt; t Map
<span class="hljs-variable">@type</span> key() <span class="hljs-symbol">:</span><span class="hljs-symbol">:</span> any()
<span class="hljs-variable">@type</span> value() <span class="hljs-symbol">:</span><span class="hljs-symbol">:</span> any()
</code></pre>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x77E5;&#x9053;&#x4E86;&#x5728; <code>Map</code> &#x7684;&#x5B9E;&#x73B0;&#x4E2D;&#x5B9A;&#x4E49;&#x4E86; <code>key</code> &#x548C; <code>value</code> &#x7C7B;&#x578B;.
&#x5BF9;&#x5E94;&#x7684;&#x5728; <code>Map</code> &#x7684;&#x6E90;&#x6587;&#x4EF6;&#x4E2D;:</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Map</span></span> <span class="hljs-keyword">do</span>
<span class="hljs-comment"># ...</span>
  <span class="hljs-variable">@type</span> key <span class="hljs-symbol">:</span><span class="hljs-symbol">:</span> any
  <span class="hljs-variable">@type</span> value <span class="hljs-symbol">:</span><span class="hljs-symbol">:</span> any
<span class="hljs-comment"># ...</span>
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x4F8B;&#x5B50;, &#x8BF4;&#x660E;&#x5728;&#x6BCF;&#x4E2A;&#x5B9E;&#x73B0;&#x4E2D; <code>key</code> &#x548C; <code>value</code> &#x53EF;&#x4EE5;&#x662F;&#x4EFB;&#x610F;&#x7C7B;&#x578B;&#x7684;&#xFF0C; &#x77E5;&#x9053;&#x8FD9;&#x4E9B;&#x662F;&#x6709;&#x5E2E;&#x52A9;&#x7684;.</p>
<p>&#x901A;&#x8FC7;&#x4F7F;&#x7528;&#x8FD9;&#x4E9B;&#x5185;&#x5EFA;&#x7684;&#x8F85;&#x52A9;&#x51FD;&#x6570;&#x6211;&#x4EEC;&#x80FD;&#x66F4;&#x8F7B;&#x677E;&#x5730;&#x6D4F;&#x89C8;&#x4EE3;&#x7801;&#x5E76;&#x4E14;&#x66F4;&#x591A;&#x5730;&#x4E86;&#x89E3;&#x5176;&#x4E2D;&#x7684;&#x8FD0;&#x884C;&#x673A;&#x5236;. IEx &#x662F;&#x4E00;&#x4E2A;&#x5F3A;&#x5927;&#x4E14;&#x5065;&#x58EE;&#x7684;&#x5DE5;&#x5177;. &#x5DE5;&#x5177;&#x7BB1;&#x4E2D;&#x6709;&#x4E86;&#x8FD9;&#x4E9B;&#x5DE5;&#x5177;, &#x6D4F;&#x89C8;&#x4EE3;&#x7801;, &#x5199;&#x7A0B;&#x5E8F;&#x53EF;&#x4EE5;&#x53D8;&#x7684;&#x66F4;&#x52A0;&#x6709;&#x8DA3;!</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="mix-tasks.html" class="navigation navigation-prev " aria-label="Previous page: 自定义Mix任务">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../advanced/erlang.html" class="navigation navigation-next " aria-label="Next page: 和Erlang互操作">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"version":"1.0.1","title":"IEx辅助函数","level":"1.18","depth":1,"next":{"title":"和Erlang互操作","level":"2.1","depth":1,"path":"lessons/advanced/erlang.md","ref":"lessons/advanced/erlang.md","articles":[]},"previous":{"title":"自定义Mix任务","level":"1.17","depth":1,"path":"lessons/basics/mix-tasks.md","ref":"lessons/basics/mix-tasks.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"lessons/basics/iex-helpers.md","mtime":"2019-11-05T02:25:51.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-11-05T03:01:22.004Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

